<?php

namespace Beerus\Modules\DecoCompany\Actions;

use Doraemon\cache\Queue;
use Doraemon\service\DecoCompanyEvaluationService;
use Doraemon\service\Sysmsg;
use Encore\Admin\Actions\RowAction;
use Illuminate\Http\Request;
use Doraemon\model\DecoCompanyEvaluation;


class delEvaluation extends RowAction
{
    public $name = '删除评价';
    const TOAST_ALERT = '确认将该评价删除嘛?删除后评价将不再生效!';
    const MSG_DETAIL = '住友你好，我们收到用户举报，由于你发布的装修公司评价违反好好住《社区规范》，现已被删除。感谢你对好好住的理解与支持。';
    const DEFAULT_HAOHAOZHU_UID = 11941;

    public function handle($id)
    {
        //根据id拿到相关的数据信息
        $eva_info = DecoCompanyEvaluation::getById($id);
        $from_uid = $eva_info['from_uid'];
        $update_data['data_status'] = DecoCompanyEvaluation::STATUS_DELETE;
        $eva_result = DecoCompanyEvaluation::updateInfo($id,$update_data);
        if ($eva_result){
            $sysMsgParams = [
                'to_uid' => $from_uid,
                'msgCode' => 0,
                'msgParams' => [
                    'from_uid'=> self::DEFAULT_HAOHAOZHU_UID,
                    'title' => '',
                    'detail' => self::MSG_DETAIL,
                ],
            ];
            Sysmsg::sendSingleMsg($sysMsgParams);
            if ($eva_info['type'] == DecoCompanyEvaluation::TYPE_USER_EVALUATION) {
                DecoCompanyEvaluationService::reCalculateScore($eva_info['to_uid']);

                // 同步评价好评率
                Queue::insertQueue('/queue/Decocompany/AsyncFavorRating',[
                    'uid' => $eva_info['to_uid'],
                ], Queue::QUEUE_DORAEMON);
            }

            return $this->response()->success('成功删除评价(*^__^*)')->refresh();
        }else{
            return $this->response()->error('删除评价失败,请稍后重试o(TωT)o');
        }

    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function dialog()
    {
        $this->confirm(self::TOAST_ALERT);
    }
}
